package game.ghostleg;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class GhostLegAPI14newActivity extends Activity {
	protected GhostLegView mView;
	protected String IPAddress;
	protected Thread td;
	protected Button nextButton;
	protected PlayerInfo pPlayerInfo;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ghost_layout);
        int pPlayerNum = 2;

        Point size=new Point();
        Display display = getWindowManager().getDefaultDisplay();
        display.getSize(size);
        int width = size.x;//display.getWidth();
        int height = size.y;//display.getHeight();
        int hostID = 0;
        
//        PlayerInfo pPlayerInfo = new PlayerInfo("Ken",1);
        
        final Bundle bundle = this.getIntent().getExtras();
        String[] IPAddress = new String[4];
        for(int i = 0; i < 4 ; i++){
        	IPAddress[i] = "";
        }
        if(bundle.getString("self").equals("host")){
        	pPlayerInfo = new PlayerInfo(bundle.getString("name"),0,bundle.getInt("score"));
        	for(int j = 1; j < 4; j++){
        		IPAddress[j] = bundle.getStringArray("clientAddr")[j];
        	}
        }else{
        	pPlayerInfo = new PlayerInfo(bundle.getString("name"),1,bundle.getInt("score"));
        	IPAddress[0] = bundle.getString("hostAddr");
        }
        
        
        /*
        IPAddress[0]="";
        IPAddress[1]="";
        IPAddress[2]="";
        IPAddress[3]="";
        */
        
        mView = (GhostLegView) findViewById(R.id.ghostleg);
        mView.setScreenSize(width,height);
        mView.setIPAddress(IPAddress);
        mView.startSetup(hostID, pPlayerInfo, pPlayerNum);
        mView.setConfirmButton((Button)findViewById(R.id.confirmButton));
        mView.setServerSocket();
        nextButton = (Button)findViewById(R.id.returnButton);
        nextButton.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		bundle.putInt("score"+mView.playerInfo.getPlayerID(),mView.playerInfo.getScore());
        		System.err.printf("%s %d\n","score"+mView.playerInfo.getPlayerID(),mView.playerInfo.getScore());
        		GhostLegAPI14newActivity.this.finish(); 
        	}
        });
        nextButton.setVisibility(View.INVISIBLE);
        mView.setNextButton(nextButton);
        mView.start();
        td = new Thread(mView);
        td.start();
    }

	
    
}